package com.infore.exchange.main.dao;

import com.infore.common.model.entity.exchange.TExTransformLog;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.infore.common.model.entity.vo.exchange.TExTransformLogVo;
import com.infore.common.request.query.exchange.TransformLogRequest;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 数据交换日志 Mapper 接口
 * </p>
 *
 * @author lijh
 * @since 2023-02-21
 */
@Mapper
@Component
public interface TExTransformLogMapper extends BaseMapper<TExTransformLog> {


    @Select("select sum(read_rows) as sendSum,sum(writer_rows) as receiveSum from t_ex_transform_log where read_rows>0")
    Map<String, Object> queryDataTransformReadAndWriterRows();


    @Select("select a.task_name,b.* from t_ex_task_info a inner join t_ex_transform_log b on a.task_id=b.task_id \n" +
            "where start_time> DATE_SUB(CURDATE(), INTERVAL '60' DAY) and exe_staus=0\n" +
            "union all \n" +
            "select a.task_name,b.* from t_ex_task_info a inner join t_ex_transform_log b on a.task_id=b.task_id \n" +
            "where  start_time> DATE_SUB(CURDATE(), INTERVAL '120' DAY) and exe_staus=1\n" +
            "order by exe_staus, start_time desc limit 100")
    List<TExTransformLogVo> queryTransformLogByLastday();


    @Select("select DATE_FORMAT(start_time,'${format}') as dateStr,sum(read_rows) as readRows,sum(writer_rows) as writerRows  from t_ex_transform_log\n" +
            "where start_time> DATE_SUB(CURDATE(), INTERVAL '${days}' DAY)\n" +
            "group by DATE_FORMAT(start_time,'${format}')\n" +
            "order by  DATE_FORMAT(start_time,'${format}')")
    List<Map<String,Object>> queryTransformLogReadWriterStatistics(@Param("format") String format, @Param("days") Integer days);



    @Select("<script>" +
            "select job_name as jobName,task_name as taskName,task_type as taskType,log.transform_name as transformName,\n" +
            "DATE_FORMAT(log.start_time,'%Y-%m-%d %H:%i:%s') as startTime,DATE_FORMAT(log.end_time,'%Y-%m-%d %H:%i:%s') as endTime," +
            "timestampdiff(second, log.start_time, log.end_time) as sec,\n" +
            "read_rows as readRows,writer_rows as writerRows,exe_staus as staus from t_ex_transform_log log\n" +
            "left join t_ex_job_info job on log.job_id=job.job_id\n" +
            "left join t_ex_task_info task on log.task_id=task.task_id " +
            "where 1=1 " +
            " <if test='transformId != null and transformId != \"\"'> "+
            "  and transform_id=#{transformId} " +
            " </if> " +
            " <if test='startDate != null and startDate != \"\"'> "+
            "  and DATE_FORMAT(start_time,'%Y-%m-%d') &gt;=#{startDate} " +
            " </if> " +
            " <if test='endDate != null and endDate != \"\"'> "+
            "  and DATE_FORMAT(end_time,'%Y-%m-%d') &lt;=#{endDate} " +
            " </if> " +
            " <if test='searchWord != null and searchWord != \"\"'> "+
            "  and (job_name like '%${searchWord}%' or task_name  like '%${searchWord}%' or transform_name like '%${searchWord}%') " +
            " </if> " +
            " <if test='exeStaus != null '> "+
            "  and exe_staus = #{exeStaus} " +
            " </if> " +
            " ORDER BY  start_time  DESC " +
            "</script>")
    List<Map<String,Object>> queryTransformLog(TransformLogRequest request);
}
